<?php
if(!defined('____Mysql_classs_php____')){
	define('____Mysql_classs_php____',true);
	include_once "config.php";

	function showMessage($error,$message){
		echo $error."   ".$message;
	}
	$_FLAG_USE_PCONNECT			= true;


	$_SQL_HOST					= DB_SERVER;
	$_SQL_USER					= DB_SERVER_USERNAME;
	$_SQL_PAWD					= DB_SERVER_PASSWORD;
	$_SQL_DBNM					= DB_DATABASE;

	Class DB {
		public $db_close_flag=1;
		function __construct() {
			global	$_SQL_HOST;
			global	$_SQL_USER;
			global	$_SQL_PAWD;
			global	$_SQL_DBNM;



			if($this->connect($_SQL_HOST, $_SQL_USER, $_SQL_PAWD, $_SQL_DBNM)){
				$this->db_close_flag=0;
			}
		}

		function __destruct() {

			$this->close();
			//echo "close..<br //>";
		}



		function connect($dbhost, $dbuser, $dbpw, $dbname)
		{
			global	$_FLAG_USE_PCONNECT;
			global	$_SQL_CHAR_SET;

			if ($_FLAG_USE_PCONNECT)
			{
				@mysql_connect ($dbhost, $dbuser, $dbpw);
			}else
			{
				@mysql_pconnect($dbhost, $dbuser, $dbpw);
			}
			if(mysql_errno()!=0){
				showMessage("Error!","connect db error :".$dbhost." ".$dbuser." ".$dbpw);
			}
			if($this->server_info() > '4.1' && $_SQL_CHAR_SET)
			{
				mysql_query("SET NAMES '".$_SQL_CHAR_SET."'");
			}
			if($this->server_info() > '5.0')
			{
				mysql_query("SET sql_mode=''");
			}
			if($dbname)
			{
				if (!@mysql_select_db($dbname))
				{
					showMessage("Error!","Select db error!".$dbname);
				}
			}
		}

		function close()
		{
			if($this->db_close_flag==0){
				$this->db_close_flag=1;
				return mysql_close();
			}
			return true;
		}

		function select_db($dbname)
		{
			if (!@mysql_select_db($dbname))
			{
				showMessage("Error!","Connect db error!".$dbname);
			}
		}

		function server_info()
		{
			return mysql_get_server_info();
		}

		function query($SQL)
		{
			$query = mysql_query($SQL);

			if (!$query)
			{
				showMessage("Error!","fetch result set error!:".$SQL);
			}
			return $query;
		}

		function GetOne($SQL)
		{

			$query=$this->query($SQL);

			$rs =& mysql_fetch_array($query, MYSQL_ASSOC);

			return $rs;
		}

		function NewUpdate($SQL_1,$SQL_2,$SQL_3)
		{
			$rt=$this->GetOne($SQL_1);
			if($rt)
			{
				$this->update($SQL_2);
			}
			else{
				$this->update($SQL_3);
			}
		}

		function update($SQL)
		{
			$query = mysql_query($SQL);

			if (!$query)
			{
				showMessage("Error!","update data error!:".$SQL);
				$query=0;
			}
			return $query;
		}

		function fetch_array($query, $result_type = MYSQL_ASSOC)
		{
			return mysql_fetch_array($query, $result_type);
		}

		function affected_rows()
		{
			return mysql_affected_rows();
		}

		function num_rows($query)
		{
			$rows = mysql_num_rows($query);
			return $rows;
		}

		function free_result($query)
		{
			return mysql_free_result($query);
		}

		function insert_id()
		{
			$id = mysql_insert_id();
			return $id;
		}

	}

	//$GLOBALS["db"] = new DB;
}
?>